package demo2;

import java.lang.reflect.Array;
import java.util.Arrays;
public class List implements AllList{
      public   int [] array;
       public int writesize;
        public static final int  n=10;
        public List(){
            this.array=new int[n];//创建长度为10的数组
        }
        public boolean full(){
            return this.writesize==array.length;
        }
        public void grow(){
            this.array=Arrays.copyOf(this.array,this.array.length*2);//扩大数组长度
        }
    @Override
    public void add(int date1) {
        if(full()){
            grow();
        }
        this.array[writesize]=date1;
        writesize++;
    }
    public void checkpos(int pos)throws PosIllegal{
            if(pos<0||pos>this.writesize){
                throw new PosIllegal("pos位置不合法------");
            }
    }

    @Override
    public void add(int date1, int pos) {
        try{
            checkpos( pos);
            if(full()){
                grow();
            }
            for(int i=this.writesize-1;i>=pos;i--){
                this.array[i+1]=this.array[i];
            }
            this.array[pos]=date1;
          this.writesize++;
        }catch (PosIllegal e){
            System.out.println("插入元素的位置不合法-----");
            e.printStackTrace();
        }
    }
    public boolean contain(int date){
            for(int i=0;i<writesize;i++){
            if (array[i]==date){
               return true;
                }
            }
            return false;
    }

    @Override
    public int find(int date) {
        for(int i=0;i<this.writesize;i++){
            if(array[i]==date){

                return i;
            }
        }
        System.out.println("没找到------");
        return -1;
    }
    public  void checkempty()throws Empty{
            if(this.writesize==0){
                throw new Empty("顺序表为空！！！");

            }
    }

    @Override
    public int get(int pos) {
        try {
            checkempty();
            checkpos(pos);
            return array[pos];
        }catch (Empty e){
            System.out.println("数组为空！");
            e.printStackTrace();
        }catch (PosIllegal e){
            System.out.println("下标位置pos不合法------");
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public void set(int pos, int date) {    //修改元素要判空与下标的合法性[java中set与get方法常用于控制对成员变量的访问与修改]
        try{
            checkempty();
            checkpos(pos);
            this.array[pos]=date;
        }catch (PosIllegal e){
            System.out.println("下标位置不合法------");
            e.printStackTrace();
        }catch (Empty e){
            System.out.println("顺序表长度为0");
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int date3) {
        try {
            checkempty();
            int n=find(date3);
            if(n==-1){
                return;
            }
            for(int i=n;i<writesize-1;i++){
                array[i]=array[i+1];
            }
            System.out.println(date3+"已经成功删除------");
            this.writesize--;
        }catch (Empty e){
            e.printStackTrace();
            System.out.println("数组为空！！！");
        }

    }

    @Override
    public int size() {
        return this.writesize;
    }

    @Override
    public void clear() {
        writesize=0;
    }

    @Override
    public void display() {
        for(int i=0;i<this.writesize;i++){
            System.out.println(array[i]+" ");
        }
    }
}
